<?php
include_once("Constants.php");
include_once("Entity/user.php");

class user_dao_base {

	function delete_user($id)
	{
		try
		{
			$dbh = new PDO(Constants::$connectionString, Constants::$username, Constants::$password);
			$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$dbh->beginTransaction();

			$stmt = $dbh->prepare("DELETE FROM user WHERE user_id = :user_id");
			$stmt->bindParam(':user_id', $id, PDO::PARAM_INT);
			$stmt->execute();
			
			$dbh->commit();
			$dbh = null;
		}
		catch(PDOException $e)
		{
			echo '<br />' . $e->getMessage();
			$dbh->rollback();
		}	
	}

	function get_user($id)
	{
		try
		{
			$dbh = new PDO(Constants::$connectionString, Constants::$username, Constants::$password);
			$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$dbh->beginTransaction();
			
			$stmt = $dbh->prepare("SELECT * FROM  user u WHERE u.user_id = :user_id");
			$stmt->bindParam(':user_id', $id, PDO::PARAM_INT);
			$stmt->execute();
			
			$u = null;
			if($row = $stmt->fetch())
			{
				$u = new user($row["user_id"],$row["name"],$row["password"],$row["address"],$row["phone"],$row["email"],$row["fax"]);
			}
			
			$dbh->commit();
			$dbh = null;
			return $u;
		}
		catch(PDOException $e)
		{
			echo '<br />' . $e->getMessage();
			$dbh->rollback();
		}	
	}
	
	function get_user_list()
	{
		try
		{
			$dbh = new PDO(Constants::$connectionString, Constants::$username, Constants::$password);
			$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$dbh->beginTransaction();

			$aou = array();
			$stmt = $dbh->query("SELECT * FROM user");
			$rows = $stmt->fetchAll();
			foreach($rows as $row)
			{
				$u = new user($row["user_id"],$row["name"],$row["password"],$row["address"],$row["phone"],$row["email"],$row["fax"]);
				$aou[] = $u;
			}

			$dbh->commit();
			$dbh = null;
			
			return $aou;
		}
		catch(PDOException $e)
		{
			echo '<br />' . $e->getMessage();
			$dbh->rollback();
		}	
	}

	function insert_user($u)
	{
		try
		{
			$dbh = new PDO(Constants::$connectionString, Constants::$username, Constants::$password);
			$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$dbh->beginTransaction();

			$stmt = $dbh->prepare("INSERT INTO user (name, password, address, phone, email, fax)
							VALUE(:name ,:password ,:address ,:phone ,:email ,:fax)");
							
							
			$stmt->bindParam(':name', $u->name);
			$stmt->bindParam(':password', $u->password);
			$stmt->bindParam(':address', $u->address);
			$stmt->bindParam(':phone', $u->phone);
			$stmt->bindParam(':email', $u->email);
			$stmt->bindParam(':fax', $u->fax);
			
			$stmt->execute();
			$id = $dbh->lastInsertId();	
			$dbh->commit();
			$dbh = null;

			return $id;
		}
		catch(PDOException $e)
		{
			echo '<br />' . $e->getMessage();
			$dbh->rollback();
		}	
	}

	function update_user($u)
	{
		try
		{
			$dbh = new PDO(Constants::$connectionString, Constants::$username, Constants::$password);
			$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$dbh->beginTransaction();

			$stmt = $dbh->prepare("UPDATE user u 
						SET 
						u.name = :name,
						u.password = :password,
						u.address = :address,
						u.phone = :phone,
						u.email = :email,
						u.fax = :fax
						WHERE 
						u.user_id = :user_id");
			$stmt->bindParam(':name', $u->name);
			$stmt->bindParam(':password', $u->password);
			$stmt->bindParam(':address', $u->address);
			$stmt->bindParam(':phone', $u->phone);
			$stmt->bindParam(':email', $u->email);
			$stmt->bindParam(':fax', $u->fax);
			$stmt->bindParam(':user_id', $u->user_id);
			
			$stmt->execute();
			
			$dbh->commit();
			$dbh = null;
		}
		catch(PDOException $e)
		{
			echo '<br />' . $e->getMessage();
			$dbh->rollback();
		}	
	}

}
?>